#pragma once
#ifndef __THREAD_HPP__
#define __THREAD_HPP__

#include <iostream>
#include <functional>
#include <string>
#include <pthread.h>

namespace ThreadModule
{
	template<typename T>
	using func_t = std::function<void(T&)>;

	template<typename T>
	class Thread
	{
	public:
		// void Excute()
		// {
		//     __func(_data);
		// }

	public:
		Thread(func_t<T> func, T& data)
			: __func(func), _data(data)
		{
			if (pthread_create(&_tid, nullptr, threadroutine, this) != 0) {
				std::cerr << "Failed to create thread!" << std::endl;
			}
		}

		static void* threadroutine(void* args)
		{
			Thread<T>* self = static_cast<Thread<T> *>(args);
			self->__func(self->_data);
			return nullptr;
		}

		void Detach()
		{
			pthread_detach(_tid);
		}

		void Join()
		{
			pthread_join(_tid, nullptr);
		}


		~Thread() {}

	private:
		pthread_t _tid;
		T& _data;
		func_t<T> __func;
	};
} // namespace ThreadModule